//from: http://www.robocommunity.com/forum/thread/14761/Rovio-API-use-examples/

import java.io.*;
import java.net.*;

public class Test {

	public static void main(String[] args) throws IOException {

		final String login = "rovio";
		final String password = "123456";

		// Create an authenticator to provide username and password to Rovio
		Authenticator.setDefault(new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(login, password.toCharArray());
			}
		});

		// Create the command being sent to Rovio
		// URL url = new
		// URL("http://192.168.1.5/rev.cgi?Cmd=nav&action=18&drive=13");

		Test exampl = new Test();
			exampl.go("http://192.168.1.5/rev.cgi?Cmd=nav&action=18&drive=1&speed=1",11,200);
			exampl.go("http://192.168.1.5/rev.cgi?Cmd=nav&action=18&drive=0",1,1000);
		// exampl.go("http://192.168.1.5/rev.cgi?Cmd=nav&action=18&drive=5&speed=8",2);
		// exampl.go("http://192.168.1.5/rev.cgi?Cmd=nav&action=18&drive=17&speed=5",49);
		// exampl.go("http://192.168.1.5/rev.cgi?Cmd=nav&action=18&drive=2&speed=10",50);
		// exampl.go("http://192.168.1.5/rev.cgi?Cmd=nav&action=13",1);

	}

	public synchronized void go(String urlstring, int para, int duration) throws IOException {
		URL url = new URL(urlstring);

		for (int i = 0; i < para; i++) {
			// Open the connection to Rovio and send command
			URLConnection uc = url.openConnection();
			uc.setDoOutput(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					uc.getInputStream()));

			// Read anything returned by Rovio and print it
			String line;
			while ((line = in.readLine()) != null) {
				System.out.println(line);
			}

			// Close connection
			in.close();

			try {
				Thread.sleep(duration);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}